"""
 Command for backup database
"""

import os, subprocess, time
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = "Backup database. Only MySQL and Postgresql engines are implemented"
    
    def handle(self, *args, **options):
        from amscms import settings
        
        self.engine = settings.DATABASES['default']['ENGINE'] 
        self.db = settings.DATABASES['default']['NAME']
        self.user = settings.DATABASES['default']['USER']
        self.passwd = settings.DATABASES['default']['PASSWORD']
        self.host = settings.DATABASES['default']['HOST']
        self.port = settings.DATABASES['default']['PORT']
        
        backup_dir = settings.DATABASE_BACKUP_DIRECTORY
        outfile = os.path.join(backup_dir, 'backup_%s.sql' % time.strftime('%y%m%d%S'))
        
        if self.engine == 'django.db.backends.mysql':
            print 'Doing MySQL backup to database %s into %s' % (self.db, outfile)
            self.do_mysql_backup(outfile)
        elif self.engine in ('django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql'):
            print 'Doing Postgresql backup to database %s into %s' % (self.db, outfile)
            self.do_postgresql_backup(outfile)    
        else:
            print 'Backup in [%s] engine not implemented' % self.engine
            
    def do_mysql_backup(self, outfile):
        args = []
        if self.user:
            args += ["--user=%s" % self.user]
        if self.passwd:
            args += ["--password=%s" % self.passwd]
        if self.host:
            args += ["--host=%s" % self.host]
        if self.port:
            args += ["--port=%s" % self.port]
        args += [self.db]
        
        os.system('mysqldump %s > %s' % (' '.join(args), outfile))
        
    def do_postgresql_backup(self, outfile):
        args = []
        if self.user:
            args += ["--username=%s" % self.user]
        if self.passwd:
            args += ["--password"]
        if self.host:
            args += ["--host=%s" % self.host]
        if self.port:
            args += ["--port=%s" % self.port]
        if self.db:
            args += [self.db]
        pipe = subprocess.Popen('pg_dump %s > %s' % (' '.join(args), outfile))
        if self.passwd:
            pipe.tochild.write('%s\n' % self.passwd)
            pipe.tochild.close()

            